import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hotwork/Myapplication.dart';
import 'package:logger/logger.dart';

class AreaSelection3 extends StatefulWidget {
  final Function onSelect;
  final int initProviceIndex;
  // final int initCountyIndex;

  final int initCityIndex;

  AreaSelection3(
      {Key key,
      @required this.onSelect,
      this.initProviceIndex,
      this.initCityIndex
      // , this.initCountyIndex
      })
      : super(key: key);

  @override
  _AreaSelectionState createState() => _AreaSelectionState();
}

class _AreaSelectionState extends State<AreaSelection3> {
  ///选中的省份的index
  int selectedProvice = 0;

  ///选中的市的index
  int selectedCity = 0;

  ///定义省份控制器
  FixedExtentScrollController proviceCotroller;

  ///定义市控制器
  FixedExtentScrollController cityController;

  // ///定义区控制器
  // FixedExtentScrollController countyController;

  /// 选择变化时候的值
  Map tempAreaInfo;

  List list;

  @override
  void initState() {
    super.initState();
    selectedProvice = widget.initProviceIndex ?? 0;
    selectedCity = widget.initCityIndex ?? 0;
    list = MyApplication.mapRoadChoice3["result"];

    Logger().d(list.toList());
    proviceCotroller = new FixedExtentScrollController(
        initialItem: widget.initProviceIndex ?? 0);
    cityController =
        new FixedExtentScrollController(initialItem: widget.initCityIndex ?? 0);
    // countyController = new FixedExtentScrollController(initialItem: widget.initCountyIndex ?? 0);
    tempAreaInfo = {
      'id': list[selectedProvice]["hotUserTreeVos"][selectedCity]["id"],
      'address': list[selectedProvice]["name"] +
          "/" +
          list[selectedProvice]["hotUserTreeVos"][selectedCity]["name"],
      'proviceIndex': selectedProvice,
      'cityIndex': selectedCity,
    };
    // ignore: unnecessary_statements, unnecessary_statements
  }

  ///给父组件传递结果
  void passParams() {
    setState(() {
      tempAreaInfo = {
        'id': list[selectedProvice]["hotUserTreeVos"][selectedCity]["id"],
        'address': list[selectedProvice]["name"] +
            "/" +
            list[selectedProvice]["hotUserTreeVos"][selectedCity]["name"],
        'proviceIndex': selectedProvice,
        'cityIndex': selectedCity,
      };
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: Column(
        children: <Widget>[
          Container(
            color: Colors.white,
            padding: EdgeInsets.all(10.0),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                GestureDetector(
                    onTap: () => Navigator.of(context).pop(),
                    child: Text('取消')),
                GestureDetector(
                  onTap: () {
                    widget.onSelect(tempAreaInfo);
                    Navigator.of(context).pop();
                  },
                  child: Text(
                    '确认',
                    style: TextStyle(color: Colors.red, fontSize: 12),
                  ),
                ),
              ],
            ),
          ),
          Expanded(
            child: Row(
              children: <Widget>[
                Expanded(
                  flex: 1,
                  child: CupertinoPicker(
                    backgroundColor: Colors.white,
                    diameterRatio: 1.1,
                    scrollController: proviceCotroller,
                    itemExtent: 48.0,
                    onSelectedItemChanged: (position) {
                      setState(() {
                        selectedProvice = position;
                        cityController.jumpToItem(0);
                        passParams();
                      });
                    },
                    children: createEachItem(list),
                  ),
                ),
                Expanded(
                  flex: 1,
                  child: CupertinoPicker(
                    backgroundColor: Colors.white,
                    scrollController: cityController,
                    itemExtent: 48.0,
                    onSelectedItemChanged: (position) {
                      setState(() {
                        selectedCity = position;
                        passParams();
                      });
                    },
                    children: createEachHotuserItem(
                        list[selectedProvice]["hotUserTreeVos"]),
                  ),
                ),
              ],
            ),
          )
        ],
      ),
    );
  }
}

List<Widget> createEachItem(List data) {
  List<Widget> target = [];
  for (Map item in data) {
    target.add(Container(
      padding: EdgeInsets.only(top: 14.0, bottom: 10.0),
      child: Text(
        item["name"] ?? '',
        style: TextStyle(fontSize: 14.0),
      ),
    ));
  }
  return target;
}

List<Widget> createEachHotuserItem(List data) {
  List<Widget> target = [];
  for (Map item in data) {
    target.add(Container(
      padding: EdgeInsets.only(top: 14.0, bottom: 10.0),
      child: Text(
        item["name"] ?? '',
        style: TextStyle(fontSize: 14.0),
      ),
    ));
  }
  return target;
}
